{% extends 'base-layer.html' %}
{% load staticfiles %}

{% block css %}
    <link href="https://cdn.bootcdn.net/ajax/libs/multi-select/0.9.12/css/multi-select.min.css" rel="stylesheet">
    <style>
        .ms-container {
            width: 551px;
        }
    </style>
{% endblock %}
{% block main %}
    <div class="box box-danger">
        <form class="form-horizontal" id="selectUsersForm" action="" method="post">
            {% csrf_token %}
            <div class="box-body">
                <div class="col-xs-6 col-xs-offset-1">
                    <div class="row">
                        <div class="col-sm-6 p-r-lg">
                            <button type="button" class="selectAll btn btn-link pull-left" style="margin-bottom: 10px;">
                                选择全部
                            </button>
                        </div>
                        <div class="col-sm-6 p-r-0">
                            <button type="button" class="deselectAll btn btn-link pull-left">取消全部</button>
                        </div>
                    </div>

                    <select id="optgroup" multiple="multiple">
                        {% for item in line_list %}
                            <option value="{{ item.province }}{{ item.city }}">
                                {{ item.province }}{{ item.city }}({{ item.nums }})
                            </option>
                        {% endfor %}
                    </select>
                </div>

            </div>

            <div class="box-footer" style="display: none;">
                <div class="row span7 text-center ">
                    <button type="button" id="btnCancel" class="btn btn-default margin-right ">重置</button>
                    <button type="button" onclick="btnSave()" class="btn btn-info margin-right ">保存</button>
                </div>
            </div>
        </form>
    </div>
{% endblock %}

{% block javascripts %}
    <script src="https://cdn.bootcdn.net/ajax/libs/multi-select/0.9.12/js/jquery.multi-select.min.js"></script>
    <script type="text/javascript">
        // 初始化
        $('#optgroup').multiSelect({
            selectableHeader: '<input class="left-search-input se-con form-control" type="text" placeholder="可选线路" >',//搜索框
            selectionHeader: '<input class="col-sm-12 right-search-input se-con form-control" type="text" disabled placeholder="已选线路">', //提示框
            selectableOptgroup: true,
            afterSelect: function (values) {

            },
            afterDeselect: function (values) {

            }
        });

        // 可选全选按钮
        $('button.selectAll').click(function () {
            $('#optgroup').multiSelect('select_all');
            return false;
        });

        // 已选全选按钮
        $('button.deselectAll').click(function () {
            $('#optgroup').multiSelect('deselect_all');
            return false;
        });

        // 搜索框实时查询
        $('input.left-search-input').on('input propertychange', function () {
            var inputValue = $(this).val().trim();
            var selections = $('#optgroup').siblings('.ms-container').find('.ms-selectable li.ms-elem-selectable');
            search(selections, inputValue)
        });

        $('input.right-search-input').on('input propertychange', function () {
            var inputValue = $(this).val().trim();
            var selections = $('#optgroup').siblings('.ms-container').find('.ms-selection li.ms-elem-selection');
            search(selections, inputValue)
        });

        function search(selections, inputValue) {
            $(selections).each(function () {
                var thisValue = $(this).children('span').text();
                if (thisValue.match(inputValue)) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
        }

        // 保存
        function btnSave() {
            var options = $('.ms-selection li.ms-elem-selection');
            var option_list = new Array();

            $.each(options, function (index) {
                if ($(this).css('display') !== "none") {
                    var thisValue = $(this).children('span').text();
                    thisValue = (thisValue.replace(/\s*/g, "")).split('(')[0];
                    option_list.push(thisValue);
                }

            });

            return option_list;
        }

        /*点取消刷新新页面*/
        $("#btnCancel").click(function () {
            window.location.reload();

        });
    </script>

{% endblock %}
